package com.youlu.campus.web.activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youlu.campus.activity.service.UserActivityService;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.base.interactive.vo.ActivitySignUpRequest;
import com.youlu.campus.base.interactive.vo.ActivityVO;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;

import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.entity.QQMiniUserPO;
import com.youlu.campus.entity.UniversityInfo;
import com.youlu.campus.entity.UserActivityFootPrint;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.RankingListQueryVO;
import com.youlu.campus.entity.VO.req.ActivityFeedQueryVO;
import com.youlu.campus.entity.VO.req.ActivityQaInfoVO;
import com.youlu.campus.entity.VO.req.CheckMobileVO;
import com.youlu.campus.entity.VO.req.OrderInfoQueryVO;
import com.youlu.campus.entity.VO.req.PlaceOrderV2VO;
import com.youlu.campus.entity.VO.req.PlaceOrderVO;
import com.youlu.campus.entity.VO.req.RankingListResVO;
import com.youlu.campus.entity.VO.req.UserActivityFootPrintQueryVO;
import com.youlu.campus.entity.VO.req.UserDoQaVO;
import com.youlu.campus.entity.VO.req.UserInviteCreateVO;
import com.youlu.campus.entity.VO.req.UserInviteQueryVO;
import com.youlu.campus.entity.VO.req.UserParticipateActivityInfoQueryVO;
import com.youlu.campus.entity.VO.res.UserCaInfoRes;
import com.youlu.campus.entity.card.dto.VipRedeemCodeDTO;
import com.youlu.campus.entity.domain.ActivityRewadDomain;
import com.youlu.campus.entity.domain.ShareBindingDomain;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityFootPrintService;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.organizer.ActivityOrganizerInfoService;
import com.youlu.campus.service.platform.PlatformConfigService;
import com.youlu.campus.service.qq.QQMiniUserService;
import com.youlu.campus.service.visitor.VisitorUserService;
import com.youlu.campus.web.ca.service.UserCaInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.youlu.campus.web.university.entity.UniversityPO;
import com.youlu.campus.web.university.service.UniversityService;
import com.youlu.campus.web.user.service.UserService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 活动API
 *
 * @author: zhuhuaiqi
 * @date: 2021/2/27
 */
@Slf4j
@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserCaInfoService userCaInfoService;
    @Autowired
    private VisitorUserService visitorUserService;
    @Autowired
    private UniversityService universityService;
    @Autowired
    private ActivityOrganizerInfoService activityOrganizerInfoService;
    @Autowired
    private UserService userService;
    public static UserPO defalustUserPO;
    @Autowired
    private PlatformConfigService platformConfigService;
    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;
    @Autowired
    private ActivityFootPrintService activityFootPrintService;
    @Autowired
    private QQMiniUserService qqMiniUserService;

    @Autowired
    private UserActivityService userActivityService;

    /**
     * 活动信息匿名查询
     *
     * @param id
     * @return
     */
    @GetMapping("/public/activity/query")
    public QResult anonymousQuery(@RequestParam("id") String id) {
        return QResult.success(activityService.anonymousQuery(RequestContext.getCurUser(), id));
    }


    /**
     * 活动信息列表
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/list")
    public QResult list(@RequestBody ActivityVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityService.list(req));
    }


    /**
     * 活动信息列表
     *
     * @param req
     * @return
     */
    @PostMapping("/activity/list/unfinished")
    public QResult findByPageAndUnfinished(@RequestHeader String minitoken, @RequestBody ActivityVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setMinitoken(minitoken);
        return QResult.success(activityService.findByPageAndUnfinished(req));
    }

    /**
     * 已经参与活动信息列表
     *
     * @param req
     * @return
     */
    @PostMapping("/activity/list/joined")
    public QResult findByPageAndJoined(@RequestHeader String minitoken,@RequestBody ActivityVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setUserId(RequestContext.getUserId());
        req.setMinitoken(minitoken);
        return QResult.success(activityService.findByPageAndJoined(req));
    }


    /**
     * 我参与的活动信息列表
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/my/list")
    public QResult myActivityList(@RequestBody UserParticipateActivityInfoQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityService.myActivityList(req));
    }

    /**
     * 活动信息查询
     *
     * @param id
     * @return
     */
    @GetMapping("/mini-app/activity/query")
    public QResult query(@RequestParam("id") String id) {
        return QResult.success(activityService.query(RequestContext.getCurUser(), id, Boolean.FALSE, RequestContext.getCurPlatform()));
    }

    /**
     * 线下活动信息查询
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity/offline/query")
    public QResult queryOffLine(@RequestParam("activityId") String activityId) {
        final String userId = RequestContext.getUserId();
        return QResult.success(activityService.getOffLineDetail(activityId));
    }

    /**
     * 活动报名
     *
     * @param request
     * @return
     */
    @PostMapping("/mini-app/activity/sign-up")
    public QResult signUp(@RequestBody ActivitySignUpRequest request) {
        request.setUserId(RequestContext.getUserId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setAppId(RequestContext.getAppId());
        if (Boolean.TRUE.equals(request.getMustCode())) {
            CheckMobileVO req = new CheckMobileVO();
            req.setUserPO(RequestContext.getCurUser());
            req.setCode(request.getCode());
            req.setMobile(request.getMobile());
            req.setPaltform(RequestContext.getCurPlatform());
            boolean re = userService.checkMobile(req);
            if (!re) {
                log.error(":>>> 用户验证码错误:{}", request.getUserId());
                throw new BusinessException("验证码错误");
            }
            UserPO userPO = visitorUserService.find(request.getMobile(), Boolean.TRUE);
            if (Objects.isNull(userPO)) {
                log.warn(":>>> 用户不存在多平台使用系统系统,不需要绑定");
            } else {
                if (!request.getUserId().equals(userPO.getId())) {
                    request.setUserId(userPO.getId());
                    log.info(":>>> 用户手机号:{},存在其他平台账号:{},进行更换", request.getMobile(), RequestContext.getUserId(), request.getUserId());
                    // 需要更新邀请记录的被邀请人的ID,进行邀请人的绑定
                }
            }
        }
        return QResult.success(userActivityService.signUpActivity(request, RequestContext.getCurUser()));
    }

    /**
     * 获取用户问卷信息
     *
     * @param activityId
     * @param activityQaInfoId
     * @return
     */
    @GetMapping("/mini-app/activity/user/qa")
    public QResult getUserQaInfo(@RequestParam("activityId") String activityId,
                                 @RequestParam("activityQaInfoId") String activityQaInfoId) {
        return QResult.success(userParticipateActivityInfoService.findUserQaByUserIdAndActivityId(RequestContext.getUserId(), activityId, activityQaInfoId));
    }

    @GetMapping("/mini-app/activity/sign-up/info")
    public QResult getSignUpInfo(@RequestParam("activityId") String activityId) {
        return QResult.success(activityService.getActivitySignUpInfo(RequestContext.getUserId(), "", activityId, 1));
    }

    @GetMapping("/mini-app/activity/user/sign-up")
    public QResult userInfoSignUp(@RequestParam("activityId") String activityId) {
        return QResult.success(activityService.getActivitySignUpInfo(RequestContext.getUserId(), "", activityId, 2));
    }

    @PostMapping("/mini-app/activity/sign-up/info/update")
    public QResult updateSignUpInfo(@RequestBody ActivitySignUpRequest req) {
        req.setUserId(RequestContext.getUserId());
        req.setMobile(null);
        return QResult.success(userActivityService.updateSignUpInfo(req));
    }

    /**
     * 活动高校排行榜 1000
     *
     * @return
     */
    @PostMapping("/mini-app/activity/ranking-list/university")
    public QResult getUniverityRankingList(@RequestBody RankingListQueryVO request) {
        request.setUserId(RequestContext.getUserId());
        request.setUserPO(RequestContext.getCurUser());
        if (StringUtils.isNotBlank(RequestContext.getCurUser().getUniversityId())) {
            request.setUniversityId(RequestContext.getCurUser().getUniversityId());
            UniversityPO universityPO = universityService.getById(RequestContext.getCurUser().getUniversityId());
            if (Objects.nonNull(universityPO)) {
                request.setUniversity(universityPO.getName());
                request.setUniversityId(universityPO.getId());
            }
        }
        RankingListResVO res = activityService.getUniversityContestRanking(request, RequestContext.getCurUser());
        return QResult.success(res);
    }

    /**
     * 活动用户任务排行榜 1000
     *
     * @param request
     * @return
     */
    @PostMapping("/mini-app/activity/ranking-list/user-task")
    public QResult getUserTaskRanking(@RequestBody RankingListQueryVO request) {
        request.setUserId(RequestContext.getUserId());
        request.setUserPO(RequestContext.getCurUser());
        request.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityService.getUserTaskRanking(request, RequestContext.getCurUser()));
    }

    /**
     * 活动用户任务排行榜 1000,匿名接口
     *
     * @param request
     * @return
     */
    @PostMapping("/public/activity/ranking-list/user-task")
    public QResult getAnonymousUserTaskRanking(@RequestBody RankingListQueryVO request) {
        UserPO userPO = visitorUserService.findUser("default", "wechat-h5", "default");
        if (Objects.isNull(userPO)) {
            log.error(":>>> 默认用户为空:186");
            return QResult.fail("默认用户不存在");
        }
        request.setUserId(userPO.getId());
        request.setUserPO(userPO);
        request.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityService.getUserTaskRanking(request, userPO));
    }

    /**
     * 活动申办单位任务排行榜 1000
     *
     * @param request
     * @return
     */
    @PostMapping("/mini-app/activity/ranking-list/organizer")
    public QResult getOrganizerRanking(@RequestBody RankingListQueryVO request) {
        String orgId = activityOrganizerInfoService.getOrganizerId("", RequestContext.getUserId());
        request.setUserId(RequestContext.getUserId());
        request.setUserPO(RequestContext.getCurUser());
        request.setOrganizerId(orgId);
        request.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityService.getOrganizerRanking(request, request.getUserPO()));
    }

    /**
     * 活动申办单位任务排行榜 1000,匿名接口
     *
     * @param request
     * @return
     */
    @PostMapping("/public/activity/ranking-list/organizer")
    public QResult getAnonymousOrganizerRanking(@RequestBody RankingListQueryVO request) {
        UserPO userPO = getDefaultUserPO();
        String orgId = activityOrganizerInfoService.getOrganizerId("", userPO.getId());
        request.setUserId(userPO.getId());
        request.setUserPO(userPO);
        request.setOrganizerId(orgId);
        return QResult.success(activityService.getOrganizerRanking(request, request.getUserPO()));
    }

    /**
     * 活动高校排行榜 1000
     *
     * @param request
     * @return
     */
    @PostMapping("/public/activity/ranking-list/university")
    public QResult getAnonymousUniverityRankingList(@RequestBody RankingListQueryVO request) {
        UserPO userPO = getDefaultUserPO();
        request.setUserId(userPO.getId());
        request.setUserPO(userPO);
        if (StringUtils.isNotBlank(userPO.getUniversityId())) {
            request.setUniversityId(userPO.getUniversityId());
            UniversityPO universityPO = universityService.getById(userPO.getUniversityId());
            if (Objects.nonNull(universityPO)) {
                request.setUniversity(universityPO.getName());
                request.setUniversityId(universityPO.getId());
            }
        }
        RankingListResVO res = activityService.getUniversityContestRanking2(request, userPO);
        return QResult.success(res);
    }

    /**
     * 活动邀请记录
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/invite/list")
    public QResult getUserInvites(@RequestBody UserInviteQueryVO req) {
        if (StringUtils.isBlank(req.getUserId())) {
            req.setUserId(RequestContext.getUserId());
        }
        req.setAppId(RequestContext.getAppId());
        req.setPlatform(RequestContext.getCurPlatform());
        if (StringUtils.isBlank(req.getTaskId())) {
            ActivityTaskInfo taskInfo = activityService.findActivityTaskInfo(req.getActivityId(), req.getTaskBizType());
            if (Objects.isNull(taskInfo)) {
                log.error(":>>> 活动:{} 没有配置邀请任务", req.getTaskId());
                throw new BusinessException("活动没有配置邀请任务,请咨询客服");
            }
            req.setTaskId(taskInfo.getId());
        }
        return QResult.success(activityService.getUserInviteRecord(req));
    }

    /**
     * 创建活动邀请
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/invite/create")
    public QResult createUserInvites(@RequestBody UserInviteCreateVO req) {
        log.info(":>>> 开始创建活动邀请:{}", JSON.toJSONString(req));
        String platform = RequestContext.getCurPlatform();
        String appId = RequestContext.getAppId();
        log.info(":>>> 平台:{},appId:{}", platform, appId);
        UserPO invitedByUser = null;
        switch (platform) {
            case "qq-h5":
                invitedByUser = visitorUserService.getUser(platform, appId, req.getSharedByOpenId());
                break;
            case "qq":
                QQMiniUserPO qqMiniUserPO = qqMiniUserService.queryUserByOpenId(appId, req.getSharedByOpenId());
                if (Objects.nonNull(qqMiniUserPO)) {
                    invitedByUser = new UserPO();
                    BeanUtils.copyProperties(qqMiniUserPO, invitedByUser);
                    invitedByUser.setId(qqMiniUserPO.getAccountInfoId());
                }
                break;
            case "wechat-h5":
                invitedByUser = visitorUserService.getUser(platform, appId, req.getSharedByOpenId());
                break;
            case "wechat-mini":
                invitedByUser = visitorUserService.getUser(platform, appId, req.getSharedByOpenId());
                break;
            case "douyin-mini":
                invitedByUser = visitorUserService.getUser(platform, appId, req.getSharedByOpenId());
                break;
            default:
                break;
        }
        req.setInvitedByUserInfo(invitedByUser);
        req.setTargetOpenId(RequestContext.getCurUser().getOpenId());
        req.setTargetUser(RequestContext.getCurUser());
        req.setAppId(appId);
        req.setPlatform(platform);
        return QResult.success(activityService.createActivityInvite2(req));
    }

    /**
     * 足迹列表
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/foot-print/list")
    public QResult getFootPrint(@RequestBody UserActivityFootPrintQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(activityFootPrintService.getUserActivityFootPrint(req));
    }

    /**
     * 创建足迹
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/foot-print/create")
    public QResult createFootPrint(@RequestBody UserActivityFootPrint req) {
        req.setUserId(RequestContext.getUserId());
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(activityFootPrintService.createUserActivityFootPrint(req));
    }

    /**
     * 删除足迹
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/foot-print/delete")
    public QResult deleteFootPrint(@RequestBody UserActivityFootPrint req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityFootPrintService.deleteUserActivityFootPrint(req));
    }

    /**
     * 活动答题获取题库
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/qa")
    public QResult getQa(@RequestBody ActivityQaInfoVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityService.getQa(req));
    }

    /**
     * 活动问题介绍
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/qa-info")
    public QResult getQaInfo(@RequestBody ActivityQaInfoVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityService.getQaInfo(req));
    }

    /**
     * 提交答题:针对只有一套题
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/qa-do")
    public QResult doQa(@RequestBody UserDoQaVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityService.doQa(req));
    }

    /**
     * 收藏活动
     *
     * @param id
     * @return
     */
    @GetMapping("/mini-app/activity/favorite")
    public QResult favoriteActivity(@RequestParam("id") String id) {
        return QResult.success(activityService.favoriteActivity(RequestContext.getUserId(), id, Boolean.TRUE));
    }

    /**
     * 取消收藏活动
     *
     * @param id
     * @retur
     */
    @PostMapping("/mini-app/activity/unfavorite")
    public QResult unfavoriteActivity(@RequestParam("id") String id) {
        return QResult.success(activityService.favoriteActivity(RequestContext.getUserId(), id, Boolean.FALSE));
    }

    /**
     * 领取奖励详情
     *
     * @param
     * @return
     */
    @GetMapping("/mini-app/activity/reward/detail")
    public QResult rewardDetail(@RequestParam("activityId") String activityId, @RequestParam("taskBizType") String taskBizType) {
        if ("wechat-h5".equals(RequestContext.getCurPlatform())) {
            if (StringUtils.isBlank(activityId) || "undefined".equals(activityId)) {
                activityId = "606ffa6bfb8ede29c8058639";
            }
        }
        return QResult.success(activityService.getReward(activityId, RequestContext.getUserId(), taskBizType));
    }

    /**
     * 志愿福利详情
     *
     * @param
     * @return
     */
    @GetMapping("/mini-app/activity/volunteer-welfare/detail")
    public QResult volunteerWelfareDetail(@RequestParam("activityId") String activityId) {
        ActivityInfo activityInfo = activityService.findOne(activityId);
        if (Objects.nonNull(activityInfo)) {
            return QResult.success(activityInfo.getVolunteerWelfareDetail());
        }
        return QResult.success("OK");
    }

    /**
     * 志愿福利详情
     *
     * @param
     * @return
     */
    @GetMapping("/mini-app/activity/broadcast/detail")
    public QResult broadcastDetail(@RequestParam("activityId") String activityId) {
        ActivityInfo activityInfo = activityService.findOne(activityId);
        if (Objects.nonNull(activityInfo)) {
            return QResult.success(activityInfo.getBroadcastDetail());
        }
        return QResult.success("OK");
    }

    /**
     * 领取奖励发起支付
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/reward/place-order")
    public QResult placeOrder(@RequestBody ActivityRewadDomain req, HttpServletRequest request) {
        req.setUserId(RequestContext.getUserId());
        req.setClientIp(CommonUtils.getRemoteIp(request));
        req.setSource(1);
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatfrom(RequestContext.getCurPlatform());
        if (StringUtils.isBlank(req.getUniversityId())) {
            UniversityInfo universityInfo = universityService.getUniversity(RequestContext.getCurUser(), req.getMobile());
            if (Objects.nonNull(universityInfo)) {
                req.setUniversityId(universityInfo.getId());
            }
        }
        return QResult.success(userCaInfoService.placeOrder(req));
    }

    /**
     * 是否领取奖励
     *
     * @param
     * @return
     */
    @GetMapping("/mini-app/activity/reward/check")
    public QResult isGetReward(@RequestParam(value = "activityId", required = false) String activityId,
                               @RequestParam(value = "taskBizType", required = false) String taskBizType) {
        boolean re = activityService.isGetReward(activityId, RequestContext.getUserId());
        log.info(":>>> 用户:{},活动:{},是否领取奖励:{}", RequestContext.getUserId(), activityId, re);
        return QResult.success(re);
    }

    /**
     * 活动分享绑定
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/share/binding")
    public QResult shareBinding(@RequestBody ShareBindingDomain req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityService.shareBinding(req));
    }

    /**
     * 活动任务Feed 流
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/task/feed")
    public QResult getTaskFeeds(@RequestBody ActivityFeedQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityService.getTaskFeeds(req));
    }

    /**
     * 活动任务Feed流
     *
     * @param
     * @return
     */
    @PostMapping("/public/activity/task/feed")
    public QResult getPublicTaskFeeds(@RequestBody ActivityFeedQueryVO req) {
        return QResult.success(activityService.getTaskFeeds(req));
    }

    /**
     * 获取支付的参数
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/pay-config")
    public QResult getPayConfig(@RequestParam("url") String url, @RequestBody HashMap<String, String> datas) {
        return QResult.success(activityService.getShareConfig(RequestContext.getCurUser(), url));
    }

    /**
     * 获取支付的参数
     *
     * @param
     * @return
     */
    @PostMapping("/public/app/js-sdk")
    public QResult getJsSdkByAppId(@RequestParam("appId") String appId, @RequestParam("url") String url) {
        return QResult.success(activityService.getJsSdkConfigByAppId(appId, url));
    }


    /**
     * 查询证书登记
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/query-ca")
    public QResult queryCa(@RequestParam("mobile") String mobile, @RequestParam(value = "activityId", required =
            false) String activityId, @RequestParam("taskBizType") String taskBizType) {
        return QResult.success(activityService.queryCa(mobile, activityId, taskBizType));
    }

    @PostMapping(value = "/mini-app/activity/user-ca/import")
    @ResponseBody
    public RestResult importUserCa(@RequestParam("file") MultipartFile file, @RequestParam("activityId") String activityId) {
        try {

            String fileName = file.getOriginalFilename();
            log.info(":>>> 上传文件为:{}", fileName);
            HashMap<String, Object> result = activityService.importUserCa(file, activityId);
            log.info(":>>> 批量导入活动证书详情数据结果:{}", JSON.toJSON(result));
            return RestResultBuilder.builder().success(result).build();
        } catch (com.youlu.campus.common.exception.BusinessException e) {
            log.error(":>>> 批量导入活动证书价格错误:{}", e);
            throw e;
        } catch (Exception e) {
            log.error(":>>> 批量导入活动证书错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }

    /**
     * 获取分享海报信息
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/play-bill")
    public QResult getPlayBill(@RequestParam("taskId") String taskId) {
        return QResult.success(activityService.getPlayBill(RequestContext.getCurUser(),
                taskId, RequestContext.getAppId(),
                RequestContext.getCurPlatform(), RequestContext.getCurAuthorizeMode()));
    }

    /**
     * 获取分享信息
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/share-info")
    public QResult getShareInfo(@RequestParam("activityId") String activityId, @RequestParam("taskId") String taskId) {
        return QResult.success(activityService.getPlayBill(RequestContext.getCurUser(),
                taskId, RequestContext.getAppId(),
                RequestContext.getCurPlatform(), RequestContext.getCurAuthorizeMode()));
    }

    /**
     * 数据补偿
     *
     * @param
     * @return
     */
    @PostMapping("/public/activity/data-ree")
    public QResult reeData(@RequestParam("activityId") String activityId) {
        return QResult.success(activityService.reeData(activityId, RequestContext.getCurUser()));
    }

    /**
     * 证书领取接口
     */
    @PostMapping("/mini-app/activity/ca-receive")
    public QResult receiveCa(@RequestParam("activityId") String activityId, @RequestParam(defaultValue = "0", value = "rewardType") Integer rewardType) {
        String appId = RequestContext.getAppId();
        return QResult.success(activityService.receiveCa(activityId, appId, RequestContext.getCurUser(), rewardType));
    }

    /**
     * 获取证书领取信息
     */
    @PostMapping("/mini-app/activity/ca-receive/detail")
    public QResult receiveCaDetial(@RequestParam("activityId") String activityId,
                                   @RequestParam("taskBizType") String taskBizType,
                                   @RequestParam(required = false) String mobile) {
        return QResult.success(activityService.getUserCaInfo(activityId, taskBizType, RequestContext.getUserId(),
                mobile, RequestContext.getCurUser().getOpenId()));
    }

    /**
     * 提交证书领取信息
     */
    @PostMapping("/mini-app/activity/ca-receive/info")
    public QResult updateCeceiveCaInfo(@RequestBody UserCaInfoRes req) {
        req.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(activityService.updateRecieveCaInfo(req, RequestContext.getUserId(),
                RequestContext.getCurUser().getOpenId()));
    }

    /**
     * 证书领取,证书信息
     */
    @PostMapping("/mini-app/activity/ca-receive/ca-infos")
    public QResult receiveCaInfos(@RequestParam("activityId") String activityId,
                                  @RequestParam("taskBizType") String taskBizType,
                                  @RequestParam(required = false) String mobile) {
        return QResult.success(activityService.getPreCaInfo(activityId, RequestContext.getUserId(),
                RequestContext.getCurUser().getOpenId(), taskBizType, mobile));
    }

    /**
     * 证书领取下单
     */
    @PostMapping("/mini-app/activity/ca-receive/place-order")
    public QResult placeOrder(@RequestBody PlaceOrderVO req) {
        return QResult.success(userCaInfoService.placeOrder(req, RequestContext.getCurUser()));
    }

    /**
     * 证书领取下单V2接口
     */
    @PostMapping("/mini-app/v2/activity/items/place-order")
    public QResult placeOrderV2(@RequestBody PlaceOrderV2VO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(userCaInfoService.placeOrderV2(req, RequestContext.getCurUser()));
    }

    /**
     * 证书订单查询
     */
    @PostMapping("/mini-app/activity/ca-receive/order-detail")
    public QResult orderDetail(@RequestParam("orderNo") String orderNo) {
        String platForm = RequestContext.getCurPlatform();
        return QResult.success(userCaInfoService.getOrderDetail(orderNo, RequestContext.getUserId(), platForm));
    }

    /**
     * 查询兑换码
     */
    @PostMapping("/mini-app/activity/ca-receive/query/redeem/code")
    public QResult placeOrder(@Validated @RequestBody VipRedeemCodeDTO dto) {
        return QResult.success(userCaInfoService.getRedeemCode(dto.getSubOrderNo(), RequestContext.getUserId(), RequestContext.isMobileLogin()));
    }

    /**
     * 领取奖励发起支付
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/ca-receive/pay")
    public QResult payOrder(@RequestBody ActivityRewadDomain req, HttpServletRequest request) {
        log.info("payOrder 领取奖励发起支付 req:{}", req);
        req.setUserId(RequestContext.getUserId());
        req.setClientIp(CommonUtils.getRemoteIp(request));
        req.setSource(1);
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatfrom(RequestContext.getCurPlatform());
        if (StringUtils.isBlank(req.getUniversityId())) {
            UniversityInfo universityInfo = universityService.getUniversity(RequestContext.getCurUser(), req.getMobile());
            if (Objects.nonNull(universityInfo)) {
                req.setUniversityId(universityInfo.getId());
            }
        }
        req.setUrl(platformConfigService.getBizFrontPayDomain() + "/h5/#/pages/receive-certificate-submit/");
        Map<String, String> stringStringMap = userCaInfoService.payOrder(req, RequestContext.getCurUser());
        log.info("payOrder 领取奖励发起支付 result:{}", JSONObject.toJSONString(stringStringMap));
        return QResult.success(stringStringMap);
    }

    /**
     * 领取奖励发起支付
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/ca-receive/order-status")
    public QResult payOrder(@RequestParam("orderNo") String orderNo) {
        return QResult.success(userCaInfoService.getOrderStatus(orderNo));
    }

    /**
     * 证书订单
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/activity/ca-receive/orders")
    public QResult orderInfos(@RequestBody OrderInfoQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setAppId(RequestContext.getAppId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setUserPO(RequestContext.getCurUser());
        Page<OrderInfo> res = userCaInfoService.getOrderInfos(req);
        return QResult.success(res);
    }

    @PostMapping("/public/activity/data-ree/province")
    public QResult reeDataProvince(@RequestParam("activityId") String activityId, @RequestParam("taskId") String taskId) {
        return QResult.success(activityService.reeDataProvince(activityId, taskId));
    }

    @GetMapping("/mini-app/activity/qa-task/query")
    public QResult queryQaTask(@RequestParam("id") String id, @RequestParam("no") Integer no) {
        return QResult.success(activityService.queryQaTask(id, no, RequestContext.getUserId()));
    }

    /**
     * 获取活动任务详情
     *
     * @param id
     * @param taskBizType
     * @return
     */
    @GetMapping("/mini-app/activity/task/query")
    public QResult queryQaTask(@RequestParam("id") String id, @RequestParam("taskBizType") TaskBizType taskBizType) {
        return QResult.success(activityService.queryTask(id, taskBizType));
    }

    /**
     * 提交多级答题
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/activity/qa-multi")
    public QResult doMultiQa(@RequestBody UserDoQaVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setUserPO(RequestContext.getCurUser());
        return QResult.success(activityService.doMultiQa(req));
    }

    /**
     * 获取活动详情
     *
     * @param id
     * @return
     */
    @GetMapping("/mini-app/activity/content/query")
    public QResult getActivityInfo(@RequestParam("id") String id) {
        return QResult.success(activityService.getActivityInfo(id));
    }

    /**
     * 获取首页活动
     *
     * @param platform
     * @return
     */
    @GetMapping("/public/activity/content/query")
    public QResult getPublicActivityInfo(@RequestParam("platform") String platform, @RequestParam(defaultValue = "1112166368") String appId) {
        return QResult.success(activityService.getPublicActivityInfo(platform, appId));
    }

    /**
     * 获取活动状态
     *
     * @param activityId
     * @return
     */
    @GetMapping("/public/activity/status-check")
    public QResult getActivityStatus(@RequestParam("activityId") String activityId) {
        ActivityInfo activityInfo = activityService.findOne(activityId);
        if (Objects.isNull(activityInfo)) {
            return QResult.success(1);
        }
        return QResult.success(getActivityStatus(activityInfo.getEndTime()));
    }

    private Integer getActivityStatus(Date endTime) {
        if (Objects.isNull(endTime)) {
            //活动未开始
            return 1;
        }
        Date now = new Date();
        return endTime.after(now) == true ? 2 : endTime.before(now) == true ? 3 : 1;
    }

    /**
     * 获取默认用户
     *
     * @return
     */
    private UserPO getDefaultUserPO() {
        UserPO userPO = defalustUserPO;
        if (Objects.isNull(userPO)) {
            userPO = visitorUserService.getDefaultUser();
            if (Objects.isNull(userPO)) {
                throw new BusinessException("默认用户不存在");
            }
            defalustUserPO = userPO;
        }
        return userPO;
    }
}